package UnitTests;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class CommonHandlerTest {

	public static void main(String[] args) throws IOException {
		/*String commandName = "InitialiseClientCommands";
        String command = "{\"CommandName\":\"InitialiseClientCommands\",\"CommandDescription\":\"null\",\"ExecuteMethod\":\"2\"}";
        ClientCommandResult commandExecutionResult = new ClientCommandResult();
        commandExecutionResult = ClientCommands.getInstance().ExecuteCommand(commandName, command);*/
        
			/*URL url = new URL("http://www.google.com/ig/api?weather=Saratov&amp;hl=en");
			URLConnection uc = url.openConnection();         
			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
			String inputLine;     
			String outLine = "";
			while ((inputLine = in.readLine()) != null)              
				outLine += inputLine;         
			in.close();
			Map<String, String> mp = GetDataFromFeed(outLine);*/
		
		/*URL url = new URL(ApplicationConfiguration.getInstance().GetWeatherFeedURI());
		URLConnection uc = url.openConnection();         
		BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
		String inputLine;     
		String outLine = "";
		while ((inputLine = in.readLine()) != null)              
			outLine += inputLine;         
		in.close();
		//Map<String, String> mp = GetDataFromFeed(outLine);
		ApplicationConfiguration.getInstance().GetCurrentWeatherImgPathByName("Clear"); 
		GetWeather g = new GetWeather();
		g.Execute();  
		g.GetResult();*/
		
		//Map<String,String[]> sm = SACManager.getInstance().GetLayout("User", "");
		
	}
	
	private static Map<String, String> GetDataFromFeed(String feed)
    {
		Map<String, String> result = new HashMap<String, String>();
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Document doc = null;
		
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		 try {
			doc = db.parse(new InputSource(new ByteArrayInputStream(feed.getBytes("utf-8"))));
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		boolean isProblem = doc.getElementsByTagName("problem_cause").getLength() > 0;
		if(!isProblem)
		{
			XPathFactory factory = XPathFactory.newInstance();
			XPath xpath = factory.newXPath();
			XPathExpression condition_data_expr = null;
			XPathExpression temp_c_data_expr = null;
			Node condition_data = null;
			Node temp_c_data = null;
			
			try {
				condition_data_expr = xpath.compile("//current_conditions/condition/@data");
				temp_c_data_expr = xpath.compile("//current_conditions/temp_c/@data");
			} catch (XPathExpressionException e) {
				e.printStackTrace();
			}

			try {
				condition_data = (Node)condition_data_expr.evaluate(doc, XPathConstants.NODE);
				temp_c_data = (Node)temp_c_data_expr.evaluate(doc, XPathConstants.NODE);
			} catch (XPathExpressionException e) {
				e.printStackTrace();
			}

			result.put("condition", condition_data.getNodeValue());
			result.put("temp_c", temp_c_data.getNodeValue());
	       // result.put("img_path", ApplicationConfiguration.getInstance().GetCurrentWeatherImgPathByName(condition_data.getNodeValue()));
		}
		
       return result; 
    }

}
